﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.Generic;
namespace Game
{
    public partial class Form1 : Form
    {
        private Batka batka;
        private Timer moveUp;
        private Timer moveDown;
        private Timer populator;
        private bool movingUp=false;
        private bool movingDown = false;
        private int level = 1;
        private int count = 0;
        private static Random rand = new Random() ;

        private SortedSet<Penguin> penguins = new SortedSet<Penguin>();

        public Form1()
        {
            InitializeComponent();
          
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            batka = new Batka(10,100,this);
            populator = new Timer();
            populator.Interval = 1000;
            populator.Tick += new EventHandler(populator_Tick);
            populator.Start();
        }

        private void populator_Tick(object sender, EventArgs e)
        {
            count++;
            if (count > 5)
            {
                count = 0;
                level++;
            }

            if (penguins.Count > level) return;
            
            int py = rand.Next() % this.Height;
            int px = this.Width;

            int ty = rand.Next() % this.Height;
            int tx = 0;

            Penguin p = new Penguin(new Point(px, py), new Point(tx - px, ty - py), this,penguins);
            penguins.Add(p);
        }

        private void BeginMoveUp()
        {
            if (movingUp) return;
            movingUp = true;
            moveUp = new Timer();
            moveUp.Interval = 20;
            moveUp.Tick += new EventHandler(delegate { batka.MoveUp(2); });
            moveUp.Start();
        }

        private void BeginMoveDown()
        {
            if (movingDown) return;
            movingDown = true;
            moveDown = new Timer();
            moveDown.Interval = 20;
            moveDown.Tick += new EventHandler(delegate { batka.MoveDown(2); });
            moveDown.Start();
        }
     
        private void StopMoveUp()
        {
            
            moveUp.Stop();
            movingUp = false;
        }

        private void StopMoveDown()
        {
            moveDown.Stop();
            movingDown = false;
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Up)
            {
                BeginMoveUp();
            } 
            if (e.KeyData == Keys.Down)
            {
                BeginMoveDown();
            }
            if (e.KeyData == Keys.Space)
            {
                new Arrow(batka.Y, this, penguins);
            }
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Up)
            {
                StopMoveUp();
            }
            if (e.KeyData == Keys.Down)
            {
                StopMoveDown();
            }
        }
    }
}
